#include "stdafx.h"

#include "CustomerTbl.h"


CCustomerTable::CCustomerTable()
{
	m_sName = _T("khach_hang");
}
bool CCustomerTable::checkCode(const CString& sCode, CCustomer* pCustomer)
{
	CString sQuery;
	MYSQL_ROW row = NULL;
	mysql_query(conn,"SET character_set_results=utf8");
	sQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_CUSTOMER,FIELD_CODE_CUSTOMER,sCode);
	CT2A ascii(sQuery, CP_UTF8);	
	mysql_query(conn,ascii.m_psz);
	MYSQL_RES * pResult = mysql_use_result(conn);
	bool bReturn = false;
	if (pResult)
	{
		row = mysql_fetch_row(pResult);
		if(row)
		{
			pCustomer = new CCustomer;
			pCustomer->setCode(sCode);
			pCustomer->setName(CString(CA2W(row[1], CP_UTF8)));		
			pCustomer->setAdress(CString(CA2W(row[2], CP_UTF8)));
			pCustomer->setPhoneNumber(CString(row[3]));
			pCustomer->setEmail(CString(row[4]));
			pCustomer->setDate(CString(row[5]));
			pCustomer->setSex(atoi(row[6]));
			pCustomer->setBill(CString(row[7]));
			bReturn = true;
		}
		mysql_free_result(pResult);
	}
	return bReturn;
}



bool  CCustomerTable::InsertCustomer(CCustomer* pCustomer)
{
	CString sValue;
	MYSQL_ROW row = NULL;
	
	sValue.Format(_T("( '%s',_utf8'%s', _utf8'%s','%s', '%s','%s',_utf8'%s','%s')")
		,pCustomer->getCode(),pCustomer->getName(),pCustomer->getAdress(),pCustomer->getPhoneNumber(),
		pCustomer->getEmail(),pCustomer->getDate(),pCustomer->getSex(),pCustomer->getBill());

	CSqlTable::m_sName = TABLE_NAME_CUSTOMER;
	return insertRecord(TBL_CUSTOMER,sValue);
}
 vector<CCustomer*> CCustomerTable::getListCustomerByName(CString sName)
 {
	 vector<CCustomer*> vtCustomer;

	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 if (sName ==_T(""))
		sNewQuery.Format(_T("SELECT * FROM %s.%s "),g_sDatabase,TABLE_NAME_CUSTOMER);
	 else
	 {
		sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s.%s LIKE '%s"),g_sDatabase,TABLE_NAME_CUSTOMER,TABLE_NAME_CUSTOMER,FIELD_NAME_CUSTOMER,sName);
		CString sAdd = _T("%')");
		sNewQuery += sAdd;
	 }
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = NULL;
		 while((row = mysql_fetch_row(pResult)) )
		 {
			 CCustomer* pCustomer = new CCustomer;
			 pCustomer->setCode(CString(CA2W(row[0])));
			 pCustomer->setName(CString(CA2W(row[1], CP_UTF8)));		
			 pCustomer->setAdress(CString(CA2W(row[2], CP_UTF8)));
			 pCustomer->setPhoneNumber(CString(row[3]));
			 pCustomer->setEmail(CString(row[4]));
			 pCustomer->setDate(CString(row[5]));
			 pCustomer->setSex(atoi(row[6]));
			 pCustomer->setBill(CString(row[7]));
			 vtCustomer.push_back(pCustomer);
		 }
		 mysql_free_result(pResult);
	 }
	 return vtCustomer;
 }


int CCustomerTable::getMaxcode()
{
	int nMax = 0;
	MYSQL_RES * pResult = NULL;

	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT %s ,MAX(%s)FROM %s.%s "),FIELD_CODE_CUSTOMER,FIELD_CODE_CUSTOMER,g_sDatabase,TABLE_NAME_CUSTOMER);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{
			CString sTemplate;
			sTemplate = row[1];
			sTemplate.Replace(_T("KH"),_T("0"));
			nMax = _tstoi(sTemplate);
		}
		mysql_free_result(pResult);
	}
	return nMax;
}